package com.hgxd.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgxd.entity.pojo.Humiture;

import com.hgxd.entity.res.GetHumitureVo;
import com.hgxd.entity.res.ResHumiture;
import com.hgxd.entity.vo.HumitureTimeVo;
import com.hgxd.mapper.HumitureMapper;
import com.hgxd.service.IHumitureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;


/**
 * <p>
 * 温湿度表 服务实现类
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Service("HumitureServiceImpl")
public class HumitureServiceImpl extends ServiceImpl<HumitureMapper, Humiture> implements IHumitureService {
    @Autowired
    HumitureMapper humitureMapper;

    /**
     * 根据时间和从机名称获得最大值最小值，注意此时的时间是一个一天的日期。
     *
     * @param time
     * @return
     */
    @Override
    public List<ResHumiture> maxTemperatureHumidityPerDay(String time, String name) {
        return humitureMapper.selectMaxTemperatureHumidityPerDay(time, name);
    }

    /**
     * 根据一个时间区间得到最大温湿度值
     *
     * @param humitureTimeVo
     * @return
     * @throws ParseException
     */

    @Override
    public List<List<ResHumiture>> maxTemperatureHumidityPerDays(HumitureTimeVo humitureTimeVo) throws ParseException {
        List<List<ResHumiture>> data = new ArrayList<>();
        LocalDate endTime = humitureTimeVo.getEndingTime();  // Start date
        for (LocalDate beginTime = humitureTimeVo.getBeginTime(); endTime.isAfter(beginTime); beginTime = beginTime.plusDays(1)) {
            if (maxTemperatureHumidityPerDay(String.valueOf(beginTime), humitureTimeVo.getName()) == null
                    || maxTemperatureHumidityPerDay(String.valueOf(beginTime), humitureTimeVo.getName()).isEmpty()) {

            } else {
                List<ResHumiture> stringObjectMap = maxTemperatureHumidityPerDay(String.valueOf(beginTime), humitureTimeVo.getName());
                data.add(stringObjectMap);
            }
        }
        return data;
    }

    /**
     * 根据时间和名称来获取该组织的一天的温湿度
     */
    @Override
    public List<ResHumiture> getDataByTimeAndOrganizationName(GetHumitureVo getHumitureVo) {
        return humitureMapper.getDataByTimeAndOrganizationName(getHumitureVo);
    }

    /**
     * 根据时间和名称来获取该组织的一天的温湿度 将时间转化为了毫秒值
     *
     * @param getHumitureVo
     * @return
     * @throws ParseException
     */
    @Override
    public List<ResHumiture> getDataToMillisecondByTimeAndName(GetHumitureVo getHumitureVo) throws ParseException {
        List<ResHumiture> dataByTimeAndName = humitureMapper.getDataByTimeAndOrganizationName(getHumitureVo);
        for (int i = dataByTimeAndName.size(); i > 0; i--) {
            ResHumiture resHumiture = dataByTimeAndName.get(i - 1);
            String time = resHumiture.getTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
            long milliseconds = simpleDateFormat.parse(time).getTime();
            resHumiture.setTime(String.valueOf(milliseconds));
        }

        return dataByTimeAndName;
    }

    @Override
    public List<ResHumiture> getDataByTimeAndEquipmentName(GetHumitureVo getHumitureVo) {
        return humitureMapper.getDataByTimeAndEquipmentName(getHumitureVo);
    }

    @Override
    public List<ResHumiture> getEquipmentDataByOrganization(String organizationName) {
        return humitureMapper.getEquipmentDataByOrganization(organizationName);
    }

    @Override
    public List<List<ResHumiture>> maxTemperatureHumidityPerDays2(HumitureTimeVo humitureTimeVo) {
        List<List<ResHumiture>> data = new ArrayList<>();
        LocalDate endTime = humitureTimeVo.getEndingTime();  // Start date
        for (LocalDate beginTime = humitureTimeVo.getBeginTime(); endTime.isAfter(beginTime); beginTime = beginTime.plusDays(1)) {
            if (maxTemperatureHumidityPerDay2(String.valueOf(beginTime), humitureTimeVo.getName()) == null
                    || maxTemperatureHumidityPerDay2(String.valueOf(beginTime), humitureTimeVo.getName()).isEmpty()) {

            } else {
                List<ResHumiture> stringObjectMap = maxTemperatureHumidityPerDay2(String.valueOf(beginTime), humitureTimeVo.getName());
                data.add(stringObjectMap);
            }
        }
        return data;
    }

    private List<ResHumiture> maxTemperatureHumidityPerDay2(String time, String name) {
        return humitureMapper.selectMaxTemperatureHumidityPerDay3(time, name);
    }


}
